﻿/*
 * Autore: Artur Tolstenco
 */

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;

namespace RecipesManager.MainControls.IngredientSubControls
{
    /// <summary>
    /// Interaction logic for IngredientAddWindow.xaml
    /// </summary>
    public partial class IngredientAddWindow : Window
    {
        readonly CommandBinding _closebinding = new CommandBinding(ApplicationCommands.Close, CommandCloseHandler);
        readonly CommandBinding _savebinding = new CommandBinding(ApplicationCommands.Save, CommandSaveHandler);
        readonly IngredientMainPane _pane;

        public IngredientAddWindow()
        {
            InitializeComponent();
            _pane = new IngredientMainPane {FromAnotherWindow = true, CurrentIngredientName = "", bt_salva = { IsDefault = true }, bt_chiudi = { IsCancel = true } };

            CommandManager.RegisterClassCommandBinding(typeof(IngredientAddWindow), _savebinding);
            CommandManager.RegisterClassCommandBinding(typeof(IngredientAddWindow), _closebinding);

            ingredientContentPane.Content = _pane;
        }

		public IngredientAddWindow(string name) {
			InitializeComponent();
			_pane = new IngredientMainPane {
				FromAnotherWindow = true,
				CurrentIngredientName = "",
				bt_salva = {
					IsDefault = true
				},
				bt_chiudi = {
					IsCancel = true
				}
			};


			CommandManager.RegisterClassCommandBinding(typeof(IngredientAddWindow), _savebinding);
			CommandManager.RegisterClassCommandBinding(typeof(IngredientAddWindow), _closebinding);

			ingredientContentPane.Content = _pane;
			_pane.CurrentIngredientName = name;
		}

        void Save_Context()
        {
            if(!_pane.SaveCurrentIngredient()) return;
            DialogResult = true;
            Close();
        }

        void Close_Context()
        {
            DialogResult = false;
            Close();
        }

        static void CommandSaveHandler(object target, ExecutedRoutedEventArgs e)
        {
            ((IngredientAddWindow)target).Save_Context();
            
        }

        static void CommandCloseHandler(object target, ExecutedRoutedEventArgs e)
        {
            ((IngredientAddWindow)target).Close_Context();

        }

        void IngredientAddWindow_OnClosing(object sender, CancelEventArgs e)
        {
        }

	    public void SetIngredientName(string name)
	    {
			_pane.tb_nomeIngrediente.Text = name;
	    }
    }
}